<?php

class ridersController extends manageController {
    # 创建验证码
    public function creatCodeAction(){
        $phone = (!empty($this->params['phone'])) ? $this->params['phone'] : '';
        $code = Data::getSalt(5);
        $data['mobile'] = $phone;
        $data['content'] = '您的验证码是：'.$code.' 请在5分钟内输入有效';
        $fields['code'] = $code;
        $fields['temp_phone'] = $phone;
        $fields['status'] = 1;
        $fields['createdt'] = 'now()';
        $id = $this->model->addItem($fields,'check_phone');
        if($id){
            Uoleem::sendMessages($data);
        }
    }
    /**
     * 骑手注册
     */
    public function regridersAction(){
        # 测试直接进入页面
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $table = 'riders';
            $phone = (!empty($this->params['phone'])) ? $this->params['phone'] : '';                            # 手机号
            $checkCode = (!empty($this->params['checkcode'])) ? $this->params['checkcode'] : '';                # 验证码
            $whereR = array("AND" => array("=" => array('tel'=>$phone, "isdeleted"=>0)));
            $ridersInfo = $this->model->getItemByOther($whereR,'riders');

            if($ridersInfo){
                $_SESSION['riders']['tel'] = $phone;
                $_SESSION['riders']['show'] = 1;
                $_SESSION['riders']['uid'] = $ridersInfo[0]['id'];
                $columns['temp_phone'] = $phone;
                $columns['status'] = 0;
                $whereU = array("AND" => array("=" => array('temp_phone'=>$phone, "status"=>1)));
                $this->model->updateItem($columns,$whereU,'check_phone');
                echo json_encode(['status'=>1,'msg'=>'登录成功']);
//                echo json_encode(['status'=>0,'msg'=>'该号码已被注册']);
            }else{
                # 判断验证码是否有效
                $sql = "select * from check_phone where temp_phone = '$phone' AND status = 1 order by createdt desc ";
                $res = $this->model->getAny($sql);
                if (!empty($res)) {
                    while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
                        $checkPhone[] = $row;
                    }
                }
                if(!$checkPhone){
                    echo json_encode(['status'=>0,'msg'=>'无效手机号']);
                    exit;
                }
                $begin = strtotime($checkPhone[0]['createdt']);
                $end = time();
                $difference = $this->timediff($begin,$end);
                if($difference['min'] >= 5){
                    $columns['temp_phone'] = $phone;
                    $columns['status'] = 0;
                    $whereU = array("AND" => array("=" => array('temp_phone'=>$phone, "status"=>1)));
                    $this->model->updateItem($columns,$whereU,'check_phone');
                    echo json_encode(['status'=>0,'msg'=>'验证码失效']);
                    exit;
                }

                if($checkPhone[0]['code'] == $checkCode){
                    $fields['tel'] = $phone;
                    $fields['status'] = 10;
                    $fields['createdt'] = 'now()';
                    $status = $this->model->addItem($fields,$table);
                    if($status){
                        $columns['temp_phone'] = $phone;
                        $columns['status'] = 0;
                        $whereU = array("AND" => array("=" => array('temp_phone'=>$phone, "status"=>1)));
                        $this->model->updateItem($columns,$whereU,'check_phone');
                        $_SESSION['riders']['tel'] = $phone;
                        $_SESSION['riders']['show'] = 1;
                        echo json_encode(['status'=>1,'msg'=>'恭喜您注册成功']);
                    }else{
                        echo json_encode(['status'=>0,'msg'=>'系统繁忙']);
                    }
                }else{
                    echo json_encode(['status'=>0,'msg'=>'验证码错误']);
                }
            }

            exit;
        }
        $this->view->setVariable("title", '注册');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function curl_wxAction(){
        $Config = Config::getInstance();
        $webSite = $Config->getWebSiteConfig();
        $url = $webSite['website_url'].'?md=riders&cl=riders&at=regriders';
        return Tools::getQRCode($url, false, $size = 10);
    }

    public function regriderssAction(){
        # 测试直接进入页面
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $_SESSION['riders']['tel'] = $_POST['tel'];
            $_SESSION['riders']['show'] = 1;
            Header("Location:http://zhaipei.shjieta.com.cn?md=riders&cl=riders&at=OrdersList");
//            Header("Location:http://zp.com:81/index.php?md=riders&cl=riders&at=OrdersList");
            exit;
        }
        $this->view->setVariable("title", '登录');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    function timediff($begin_time,$end_time)
    {
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        $timediff = $endtime-$starttime;
        $remain = $timediff%86400;
        $remain = $remain%3600;
        $mins = intval($remain/60);
        $secs = $remain%60;
        $res = array("min" => $mins,"sec" => $secs);
        return $res;
    }


    /**
     * 待抢单列表
     */
    public function OrdersListAction() {
//        unset($_SESSION['riders']);
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }


        $rsp_list = $this->orderInfo(2);
//
//        $table = "orders_express";
//
//        $whereR = array("AND" => array("=" => array('tel'=>$_SESSION['riders']['tel'], "isdeleted"=>0)));
//        $ridersInfo = $this->model->getItemByOther($whereR,'riders');
//        if($ridersInfo){
//            $sp = $ridersInfo[0]['service_shop_no'];
//            if($sp)
//                $shopNo = "and a.shop_no = $sp";
//            else
//                $shopNo = "";
//            $expressFieldsColumn = 'sda.distance as sda_distance,sda.receiver_name,sda.receiver_tel,sda.receiver_address as shop_delivery_address,a.id,a.shop_express_no,a.orders_id,a.shop_no,a.order_no,a.ph_no,a.express_type,a.shop_delivery_time,a.shop_pickup_time,a.express_order_no,a.express_fee,a.express_courier,a.express_mobile,a.express_longtitude,a.express_latitude,a.status';
//            $Sql = "SELECT $expressFieldsColumn from $table as a left join (select * from shop_delivery_address where status = '1' and isdeleted = 0 ) as sda ON a.orders_id = sda.orders_id where a.status = 2 and a.express_type = 'self'  $shopNo and a.express_courier = '' and a.isdeleted = 0";
//
////            echo $Sql;die;
//
//            $res = $this->model->getAny($Sql);
//            $rsp_list = [];
//            if (!empty($res)) {
//                while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
//                    $rsp_list[] = $row;
//                }
//            }
//
//            $orderWhere['AND']['=']['express_type'] = 'self';
//            $orderWhere['AND']['=']['status'] = 2;
//            $orderWhere['AND']['=']['isdeleted'] = 0;
//
//            $shopWhere['AND']['=']['status'] = 3;
//            $shopWhere['OR']['=']['status'] = 2;
//            $shopWhere['AND']['=']['isdeleted'] = 0;
//            $orderby = array('id' => 'asc');
//            $orderFieldsColumn = 'id,ph_no,delivery_time,receiver_address,shop_name,distance';
//            $ordersList = $this->model->getAll($orderWhere,'orders',$orderby,$orderFieldsColumn);
//            $storeSql = "SELECT id,front_province,front_city,front_area,front_address,longitude,latitude,store_id,storename,tel from xd_store where (status = 2 or status = 4 or status = 5)  and isdeleted = 0";
//            $res = $this->model->getAny($storeSql);
//            if (!empty($res)) {
//                while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
//                    $shopsList[] = $row;
//                }
//            }
//
//            foreach ($rsp_list as &$v){
//                foreach ($ordersList as $o){
//                    if($v['ph_no'] == $o['ph_no']){
//                        $v['orders'] = $o;
//                    }
//                }
//            }
//
//
//            foreach ($rsp_list as $k=>&$p){
//                foreach ($shopsList as $s){
//                    if($p['shop_no'] == $s['store_id']){
//                        $p['shop'] = $s;
//                    }
//                }
//            }
//
//            foreach($rsp_list as &$r){
//                $where = array('AND'=>array('='=>array('order_no' => $r['shop_express_no'],'shop_no' => $r['shop_no'],'status' => 1,'isdeleted' => 0)));
//                $data = $this->model->getItemByOther($where, 'tip_list');
//
//                if($data){
//                    foreach($data as $k=>$t){
//                        $r['tip'] += $t['price'];
//                    }
//                }
//                if($r['sda_distance']){
//                    $r['orders']['distance'] = $r['sda_distance'];
//                }
//            }
//
//        }
//        echo '<pre>';
//        var_dump($rsp_list);die;
        $newCount = $this->model->getNewsCount();
        $this->view->setVariable("type", 2);
        $this->view->setVariable("newCount", $newCount);
        $this->view->setVariable("rsp_list", $rsp_list);
        $this->view->setVariable("title", '订单列表');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/orderHistoryView.php');
        $this->view->render();
    }


    /**
     * 判断骑手状态
     */
    public function ridersTAction(){
        $tel = $_SESSION['riders']['tel'];
        if(!$tel){
            echo json_encode(['status'=>999,'initiate'=>999,'msg'=>'禁止非法抢单']);
            exit;
        }
        $whereR = array("AND" => array("=" => array('tel'=>$tel, "isdeleted"=>0)));
        $ridersInfo = $this->model->getItemByOther($whereR,'riders');
        if($ridersInfo[0]['status'] == 0 || $ridersInfo[0]['status'] > 1){
            echo json_encode(['status'=>$ridersInfo[0]['status'],'initiate'=>$ridersInfo[0]['initiate'],'msg'=>$ridersInfo[0]['remark']]);
        }else{
            echo json_encode(['status'=>$ridersInfo[0]['status'],'initiate'=>$ridersInfo[0]['initiate'],'msg'=>$ridersInfo[0]['remark']]);
        }
    }



    /**
     * 抢单
     */
    public function GrabAction(){
        $express_id = (!empty($this->params['express_id'])) ? (int) $this->params['express_id'] : '';
//        $where = array("AND" => array("=" => array("status"=>2,'id'=>$express_id, "isdeleted"=>0)));
//        $orderInfo = $this->model->getItemByOther($where,'orders_express');

        $SessionTel = $_SESSION['riders']['tel'];
        $result = $this->model->couponshareexecute($express_id,$SessionTel);

        if($result == 200){
            echo json_encode(['status'=>1,'msg'=>'抢单成功']);exit;
        }else{
            echo json_encode(['status'=>0,'msg'=>'手太慢了，该订单已被抢了']);exit;
        }
    }


   /**
     * 我的钱包
     */
    public function myWalletAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }

        $startdt = date('Y-m-d',strtotime('-1 month'));
        $enddt = date('Y-m-d');
        $where_fields = '';
        $where_fields .= " updatedt >= '$startdt 00:00:00'";
        $where_fields .= " AND updatedt <= '$enddt 23:59:59' AND ";

        $tel = $_SESSION['riders']['tel'];
        $sql = "SELECT * FROM orders_express  WHERE $where_fields isdeleted = 0 AND (status = 5  OR status = 6 OR status = 7) AND express_type = 'self' AND express_mobile = $tel";
        $res = $this->model->getAny($sql);
        if (!empty($res)) {
            while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
                $rsp_list[] = $row;
            }
        }

        $money = 0;
        if($rsp_list){
            foreach($rsp_list as $v){
                $money += $v['express_fee'];
            }
        }

        $this->view->setVariable("startdt",$startdt);
        $this->view->setVariable("enddt",$enddt);
        $this->view->setVariable("money",$money);
        $this->view->setVariable("data",$rsp_list);
        $this->view->setVariable("title", '我的钱包');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }


    /**
     * 筛选时间段内 个人收入
     */
    public function getMyWalletAction(){
        $startdt =(!empty($this->params['startdt'])) ? $this->params['startdt'] : '';
        $enddt =(!empty($this->params['enddt'])) ?  $this->params['enddt'] : '';
        $where_fields = "express_type = 'self' AND isdeleted = 0 AND (status = 5 OR status = 6 OR status = 7) AND express_mobile = '{$_SESSION['riders']['tel']}'";
        if($startdt){
            $where_fields .= " AND updatedt >= '$startdt 00:00:00'";
        }
        if($enddt){
            $where_fields .= " AND updatedt <= '$enddt 23:59:59'";
        }
        $sql = "SELECT id,ph_no,updatedt,express_fee FROM orders_express  WHERE $where_fields  order by id asc";
        $res = $this->model->getAny($sql);
        if (!empty($res)) {
            while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
                $rsp_list[] = $row;
            }
        }


        if($rsp_list){
            $money = 0;
            foreach($rsp_list as $v){
                $money += $v['express_fee'];
            }
            $moneys = $money;
        }

        if($rsp_list){
            echo json_encode(['status'=>1,'code'=>200,'result'=>$rsp_list,'moneys'=>$moneys]);
        }else{
            echo json_encode(['status'=>0,'msg'=>'暂无更多数据','code'=>4000]);
        }

    }


   /**
     * 配送中订单列表
     */
    public function dispatchingAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }

        $rsp_list = $this->orderInfo(4);
        $newCount = $this->model->getNewsCount();
        $this->view->setVariable("type", 4);
        $this->view->setVariable("newCount", $newCount);
        $this->view->setVariable("rsp_list", $rsp_list);
        $this->view->setVariable("title", '配送中订单');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/orderHistoryView.php');
        $this->view->render();
    }




    /**
     * 取货订单详情
     */
//    public function orderDetails_pickAction(){
//        if (!isset($_SESSION['riders']['tel'])) {
//            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
//            exit;
//        }
//        $table = 'orders_express';
//        $express_id = (!empty($this->params['express_id'])) ? (int) $this->params['express_id'] : '';
//        $where = ['AND'=>['='=>['status'=>3,'id'=>$express_id, "isdeleted"=>0]]];
//        $orderExpressInfo = $this->model->getItemByOther($where,$table);
//
//        # 订单
//        $whereOrder = ['AND'=>['='=>['status'=>3,'ph_no'=>$orderExpressInfo[0]['ph_no'], "isdeleted"=>0]]];
//        $orderInfo = $this->model->getItemByOther($whereOrder,'orders');
//        # 商品详情
//        $whereOrderProduct = ['AND'=>['='=>['ph_no'=>$orderExpressInfo[0]['ph_no']]]];
//        $orderProductInfo = $this->model->getItemByOther($whereOrderProduct,'orders_product');
//
//        # 门店详情
//        $whereShop = ['AND'=>['='=>['store_id'=>$orderExpressInfo[0]['shop_no']]]];
//        $shopInfo = $this->model->getItemByOther($whereShop,'xd_store');
//
//        $where_field_edit = array("AND" => array("=" => array( 'isdeleted' => 0, 'status' => 1, 'orders_id' => $orderExpressInfo[0]['orders_id'])));
//        $shop_delivery_address = $this->model->getItemByOther($where_field_edit,'shop_delivery_address');
//
//        if($shop_delivery_address && is_array($shop_delivery_address) && count($shop_delivery_address) > 0 ){
//            $orderInfo[0]['receiver'] = $shop_delivery_address[0]['receiver_name'];
//            $orderInfo[0]['receiver_tel'] = $shop_delivery_address[0]['receiver_tel'];
//            $orderInfo[0]['receiver_address'] = $shop_delivery_address[0]['receiver_address'];
//            $orderInfo[0]['distance'] = $shop_delivery_address[0]['distance'];
//        }
//        $orderExpressInfo['orderInfo'] = $orderInfo[0];
//        $orderExpressInfo['orderProductInfo'] = $orderProductInfo;
//        $orderExpressInfo['shopInfo'] = $shopInfo[0];
//
//        foreach($orderExpressInfo as &$r){
//            $where = array('AND'=>array('='=>array('order_no' => $r['shop_express_no'],'shop_no' => $r['shop_no'],'status' => 1,'isdeleted' => 0)));
//            $data = $this->model->getItemByOther($where, 'tip_list');
//            if($data){
//                $r['tip_arr'] = $data;
//            }
//        }
//
//        $this->view->setVariable("orderExpressInfo", $orderExpressInfo);
//        $this->view->setVariable("title", '订单详情');
//        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
//        $this->view->render();
//    }



    /**
     * 配送中订单详情
     */
    public function orderDetails_deliveryAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = 'orders_express';
        $express_id = (!empty($this->params['express_id'])) ? (int) $this->params['express_id'] : '';
        $where = ['AND'=>['='=>['status'=>4,'id'=>$express_id, "isdeleted"=>0]]];
        $orderExpressInfo = $this->model->getItemByOther($where,$table);

        # 订单
        $whereOrder = ['AND'=>['='=>['status'=>4,'ph_no'=>$orderExpressInfo[0]['ph_no'], "isdeleted"=>0]]];
        $orderInfo = $this->model->getItemByOther($whereOrder,'orders');

        # 商品详情
        $whereOrderProduct = ['AND'=>['='=>['ph_no'=>$orderExpressInfo[0]['ph_no']]]];
        $orderProductInfo = $this->model->getItemByOther($whereOrderProduct,'orders_product');

        # 门店详情
        $whereShop = ['AND'=>['='=>['store_id'=>$orderExpressInfo[0]['shop_no']]]];
        $shopInfo = $this->model->getItemByOther($whereShop,'xd_store');

        $where_field_edit = array("AND" => array("=" => array( 'isdeleted' => 0, 'status' => 1, 'orders_id' => $orderExpressInfo[0]['orders_id'])));
        $shop_delivery_address = $this->model->getItemByOther($where_field_edit,'shop_delivery_address');

        if($shop_delivery_address && is_array($shop_delivery_address) && count($shop_delivery_address) > 0 ){
            $orderInfo[0]['receiver'] = $shop_delivery_address[0]['receiver_name'];
            $orderInfo[0]['receiver_tel'] = $shop_delivery_address[0]['receiver_tel'];
            $orderInfo[0]['receiver_address'] = $shop_delivery_address[0]['receiver_address'];
            $orderInfo[0]['distance'] = $shop_delivery_address[0]['distance'];
        }

        foreach($orderExpressInfo as &$r){
            $where = array('AND'=>array('='=>array('order_no' => $r['shop_express_no'],'shop_no' => $r['shop_no'],'status' => 1,'isdeleted' => 0)));
            $data = $this->model->getItemByOther($where, 'tip_list');
            if($data){
                $r['tip_arr'] = $data;
            }
        }
        $orderExpressInfo['orderInfo'] = $orderInfo[0];
        $orderExpressInfo['orderProductInfo'] = $orderProductInfo;
        $orderExpressInfo['shopInfo'] = $shopInfo[0];
        $this->view->setVariable("orderExpressInfo", $orderExpressInfo);
        $this->view->setVariable("title", '订单详情');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }




    /**
     * 已完成订单列表
     */
//    public function finishOrderListAction(){
//        if (!isset($_SESSION['riders']['tel'])) {
//            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
//            exit;
//        }
//        $table = 'orders_express';
//        $tel = $_SESSION['riders']['tel'];
//        $page = (!empty($this->params['page'])) ? (int) $this->params['page'] : 1;
//        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
//        $this->view->setVariable("number_page", $number_page);
//        $this->view->setVariable("page", $page);
//
//        $where_fields = array("AND" => array("=" => array('status'=>5,'express_type'=>'self','express_mobile'=>$tel,"isdeleted" => 0)));
//        $orderby = array('id' => 'asc');
//        $expressFieldsColumn = '';
//        $rsp_list = $this->model->getAll($where_fields, $table, $orderby, $expressFieldsColumn );
//        foreach($rsp_list as &$r1){
//            $where['AND']['=']['ph_no'] = $r1['ph_no'];
//            $whereShop['AND']['=']['store_id'] = $r1['shop_no'];
//            $orders = $this->model->getItemByOther($where,'orders');
//            $shops = $this->model->getItemByOther($whereShop,'xd_store');
//            $r1['orderInfo'] = $orders[0];
//            $r1['shopInfo'] = $shops[0];
//        }
//
//
//        foreach($rsp_list as &$rs){
//            $data = array();
//            $data['address'] = $rs['orderInfo']['receiver_address'];
//            $baiduBS = new BaiduLBS();
//            $baiduBSInfo = $baiduBS::getLocation($data);
//            $baiduBSInfoArr = json_decode($baiduBSInfo, true);
//            $user_lng = $baiduBSInfoArr['result']['location']['lng'];
//            $user_lat = $baiduBSInfoArr['result']['location']['lat'];
//
//            $shopLong = $rs['shopInfo']['longitude'];
//            $shopLat = $rs['shopInfo']['latitude'];
//
//            $receiveLong = $user_lng;
//            $receiveLat = $user_lat;
//            $jl = $this->getdistanceAction($shopLong,$shopLat,$receiveLong,$receiveLat);
//            $rs['jl'] = $jl;
//        }
//
//
//        $this->view->setVariable('rsp_list',$rsp_list);
//        $this->view->setVariable("title", '已完成订单列表');
//        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
//        $this->view->render();
//    }


    /**
     * 完成-历史订单详情
     */
    public function finishOrderDetailsAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = 'orders_express';
        $express_id = (!empty($this->params['express_id'])) ? (int) $this->params['express_id'] : '';
        $type = (!empty($this->params['type'])) ? (int) $this->params['type'] : '';
        $where = ['AND'=>['='=>['id'=>$express_id, "isdeleted"=>0]]];
        $orderExpressInfo = $this->model->getItemByOther($where,$table);


        # 订单
        $whereOrder = ['AND'=>['='=>['ph_no'=>$orderExpressInfo[0]['ph_no'], "isdeleted"=>0]]];
        $orderInfo = $this->model->getItemByOther($whereOrder,'orders');

        # 商品详情
        $whereOrderProduct = ['AND'=>['='=>['ph_no'=>$orderExpressInfo[0]['ph_no']]]];
        $orderProductInfo = $this->model->getItemByOther($whereOrderProduct,'orders_product');

        # 门店详情
        $whereShop = ['AND'=>['='=>['store_id'=>$orderExpressInfo[0]['shop_no']]]];
        $shopInfo = $this->model->getItemByOther($whereShop,'xd_store');

        $where_field_edit = array("AND" => array("=" => array( 'isdeleted' => 0, 'status' => 1, 'orders_id' => $orderExpressInfo[0]['orders_id'])));
        $shop_delivery_address = $this->model->getItemByOther($where_field_edit,'shop_delivery_address');

        if($shop_delivery_address && is_array($shop_delivery_address) && count($shop_delivery_address) > 0 ){
            $orderInfo[0]['receiver'] = $shop_delivery_address[0]['receiver_name'];
            $orderInfo[0]['receiver_tel'] = $shop_delivery_address[0]['receiver_tel'];
            $orderInfo[0]['receiver_address'] = $shop_delivery_address[0]['receiver_address'];
        }

        $orderExpressInfo['orderInfo'] = $orderInfo[0];
        $orderExpressInfo['orderProductInfo'] = $orderProductInfo;
        $orderExpressInfo['shopInfo'] = $shopInfo[0];
        if($type){
            $this->view->setVariable("title", '历史订单详情');
        }else{
            $this->view->setVariable("title", '已完成订单详情');
        }

        foreach($orderExpressInfo as &$r){
            $where = array('AND'=>array('='=>array('order_no' => $r['shop_express_no'],'shop_no' => $r['shop_no'],'status' => 1,'isdeleted' => 0)));
            $data = $this->model->getItemByOther($where, 'tip_list');
            if($data){
                $r['tip_arr'] = $data;
            }
        }

        $this->view->setVariable("orderExpressInfo", $orderExpressInfo);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }


    private function orderInfo($type){
        $tel = $_SESSION['riders']['tel'];
        if($type == 3){
            $w = "(a.status = 3) and a.express_mobile = '$tel' ";
        }elseif ($type == 5){
            $w = "(a.status >= 5) and a.express_mobile = '$tel' ";
        }elseif ($type == 4){
            $w = "(a.status = 4) and a.express_mobile = '$tel' ";
        }elseif ($type == 2){
            $w = "(a.status = 2)";
        }
        $table = "orders_express";
        $expressFieldsColumn = 'c.front_address,b.distance,c.storename as shop_name,b.receiver,b.receiver_tel,b.receiver_address,a.id,a.shop_express_no,a.orders_id,a.shop_no,a.order_no,a.ph_no,a.express_type,a.shop_delivery_time,a.shop_pickup_time,a.express_order_no,a.express_fee,a.express_courier,a.express_mobile,a.express_longtitude,a.express_latitude,a.status,a.error_remark';
        $Sql = "SELECT $expressFieldsColumn from $table as a 
left join orders as b ON a.orders_id = b.id  and a.status = b.status 
left join xd_store as c on c.store_id =100046
where $w and a.express_type = 'self' and a.isdeleted = 0 order by a.createdt desc ";

//        echo $Sql;
        $res = $this->model->getAny($Sql);
        $rsp_list = [];
        if (!empty($res)) {
            while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
                $rsp_list[] = $row;
            }
        }


        if($rsp_list){
            foreach($rsp_list as &$r1){

                $where_field_edit = array("AND" => array("=" => array( 'isdeleted' => 0, 'status' => 1, 'orders_id' => $r1['orders_id'])));
                $shop_delivery_address = $this->model->getItemByOther($where_field_edit,'shop_delivery_address');
                if($shop_delivery_address && is_array($shop_delivery_address) && count($shop_delivery_address) > 0 ){
                    $r1['receiver'] = $shop_delivery_address[0]['receiver_name'];
                    $r1['receiver_tel'] = $shop_delivery_address[0]['receiver_tel'];
                    $r1['receiver_address'] = $shop_delivery_address[0]['receiver_address'];
                    $r1['distance'] = $shop_delivery_address[0]['distance'];
                }

                $where = array('AND'=>array('='=>array('order_no' => $r1['shop_express_no'],'shop_no' => $r1['shop_no'],'status' => 1,'isdeleted' => 0)));
                $data = $this->model->getItemByOther($where, 'tip_list');

                if($data){
                    foreach($data as $k=>$t){
                        $r1['tip'] += $t['price'];
                    }
                }
            }
        }
        return $rsp_list;
    }

    /**
     * 历史订单
     */
    public function orderHistoryAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $rsp_list = $this->orderInfo(5);
        $newCount = $this->model->getNewsCount();
        $this->view->setVariable("newCount", $newCount);
        $this->view->setVariable('rsp_list',$rsp_list);
        $this->view->setVariable("title", '历史订单');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }


    /**
     * 个人信息
     */
    public function personageInfoAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = 'riders';
        $tel = $_SESSION['riders']['tel'];
        $where_fields = array("AND" => array("=" => array("tel" => $tel)));
        $ridersResult = $this->model->getItemByOther($where_fields, $table);

        $orderby = array('id' => 'asc');
        $like_fields = [];
        $where['AND']['=']['parent_code'] = 1;
        $where_city['AND']['=']['is_parent'] = 1;
        $where_city['AND']['>']['parent_code'] = 1;
        $where_area['AND']['=']['is_parent'] = 0;
        $where_area['AND']['>']['parent_code'] = 1;
        $province = $this->model->getAll($where,'area',$orderby,$like_fields);
        $city = $this->model->getAll($where_city,'area',$orderby,$like_fields);
        $area = $this->model->getAll($where_area,'area',$orderby,$like_fields);

        $storeSql = "SELECT store_id,storename from xd_store where (status = 2 or status = 4 or status = 5)  and isdeleted = 0";
        $store = $this->model->getAny($storeSql);
        if (!empty($store)) {
            while ($Stores = mysqli_fetch_array($store, MYSQLI_ASSOC)) {
                $row[] = $Stores;
            }
        }
        foreach ($ridersResult as &$v1){
            foreach ($province as $p){
                if($v1['province'] == $p['code']){
                    $v1['province_name'] = $p['fullname'];
                }
                if($v1['service_province'] == $p['code']){
                    $v1['service_province_name'] = $p['fullname'];
                }
            }
        }

        foreach ($ridersResult as &$v2){
            foreach ($city as $c){
                if($v2['city'] == $c['code']){
                    $v2['city_name'] = $c['fullname'];
                }
                if($v2['service_city'] == $c['code']){
                    $v2['service_city_name'] = $c['fullname'];
                }
            }
        }

        foreach ($ridersResult as &$v3){
            foreach ($area as $a){
                if($v3['area'] == $a['code']){
                    $v3['area_name'] = $a['fullname'];
                }
                if($v3['service_area'] == $a['code']){
                    $v3['service_area_name'] = $a['fullname'];
                }
            }
        }

        foreach ($ridersResult as &$v4){
            if($row){
                foreach ($row as $r){
                    if($v4['service_shop_no'] == $r['store_id']){
                        $v4['shopname'] = $r['storename'];
                    }
                }
            }

        }

        $newCount = $this->model->getNewsCount();
        $this->view->setVariable("newCount", $newCount);
        $this->view->setVariable("ridersResult", $ridersResult[0]);
        $this->view->setVariable("title", '个人信息');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }


    /**
     * 待取货
     */
    public function assignPickAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }

        $rsp_list = $this->orderInfo(3);
//        $table = "orders_express";
//        $tel = $_SESSION['riders']['tel'];
//        $expressFieldsColumn = 'sda.distance as sdistance,sda.receiver_name,sda.receiver_tel,sda.receiver_address as shop_delivery_address,a.id,a.shop_express_no,a.orders_id,a.shop_no,a.order_no,a.ph_no,a.express_type,a.shop_delivery_time,a.shop_pickup_time,a.express_order_no,a.express_fee,a.express_courier,a.express_mobile,a.express_longtitude,a.express_latitude,a.status';
//        $Sql = "SELECT $expressFieldsColumn from $table as a left join (select * from shop_delivery_address where status = '1' and isdeleted = 0 ) as sda ON a.orders_id = sda.orders_id
//where a.status = 3 and a.express_type = 'self' and a.express_mobile = '$tel' and a.isdeleted = 0 order by a.createdt desc ";
//        $res = $this->model->getAny($Sql);
//        $rsp_list = [];
//        if (!empty($res)) {
//            while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
//                $rsp_list[] = $row;
//            }
//        }
//
//
//        if($rsp_list){
//            foreach($rsp_list as &$r1){
//                $where['AND']['=']['ph_no'] = $r1['ph_no'];
//                $whereShop['AND']['=']['store_id'] = $r1['shop_no'];
//                $orders = $this->model->getItemByOther($where,'orders');
//                $shops = $this->model->getItemByOther($whereShop,'xd_store');
//                $r1['orderInfo'] = $orders[0];
//                $r1['shopInfo'] = $shops[0];
//
//                $where = array('AND'=>array('='=>array('order_no' => $r1['shop_express_no'],'shop_no' => $r1['shop_no'],'status' => 1,'isdeleted' => 0)));
//                $data = $this->model->getItemByOther($where, 'tip_list');
//
//                if($data){
//                    foreach($data as $k=>$t){
//                        $r1['tip'] += $t['price'];
//                    }
//                }
//
//                if($r1['sdistance']){
//                    $r1['orderInfo']['distance'] = $r1['sdistance'];
//                }
//            }
//        }
//        echo '<pre>';
//        var_dump($rsp_list[0]['orderInfo']['distance']);die;
        $newCount = $this->model->getNewsCount();
        $this->view->setVariable("type", 3);
        $this->view->setVariable("newCount", $newCount);
        $this->view->setVariable('rsp_list',$rsp_list);
        $this->view->setVariable("title", '待取货');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/orderHistoryView.php');
        $this->view->render();
    }



    /**
     * 待取货订单详情
     * 接收指派单id
     */
    public function orderDetails_awaitAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = 'orders_express';
        $express_id = (!empty($this->params['express_id'])) ? (int) $this->params['express_id'] : '';
        $where = ['AND'=>['='=>['status'=>3,'id'=>$express_id, "isdeleted"=>0]]];
        $orderExpressInfo = $this->model->getItemByOther($where,$table);

        # 订单
        $whereOrder = ['AND'=>['='=>['status'=>3,'ph_no'=>$orderExpressInfo[0]['ph_no'], "isdeleted"=>0]]];
        $orderInfo = $this->model->getItemByOther($whereOrder,'orders');

        # 商品详情
        $whereOrderProduct = ['AND'=>['='=>['ph_no'=>$orderExpressInfo[0]['ph_no']]]];
        $orderProductInfo = $this->model->getItemByOther($whereOrderProduct,'orders_product');

        # 门店详情
        $whereShop = ['AND'=>['='=>['store_id'=>$orderExpressInfo[0]['shop_no']]]];
        $shopInfo = $this->model->getItemByOther($whereShop,'xd_store');

        $orderExpressInfo['orderInfo'] = $orderInfo[0];
        $orderExpressInfo['orderProductInfo'] = $orderProductInfo;
        $orderExpressInfo['shopInfo'] = $shopInfo[0];
//        echo'<pre>';
//        var_dump($orderExpressInfo);
        $this->view->setVariable("orderExpressInfo", $orderExpressInfo);
        $this->view->setVariable("title", '订单详情');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    private function getCurl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result =  curl_exec($ch);
        curl_close ($ch);
        return $result;
    }

    /**
     * 确认取货
     */
    public function assPickAction(){
        $express_id = (!empty($this->params['express_id'])) ? (int) $this->params['express_id'] : '';

        $whereExp = ['AND'=>['='=>['id'=>$express_id]]];
        $ExpInfo = $this->model->getItemByOther($whereExp,'orders_express');
        if(!$ExpInfo){
            echo json_encode(['status'=>1,'msg'=>'读取数据失败','code'=>3003]);exit;
        }
        $field['updatedt'] = 'now()';
        $field['status'] = 4;
        $where_fields = array("AND" => array("=" => array('id' => $express_id, "isdeleted" => 0)));
        $e = $this->model->updateItem($field, $where_fields, 'orders_express');


        $field = array();
        $field['updatedt'] = 'now()';
        $field['status'] = 4;
        $where_fields = array("AND" => array("=" => array('ph_no' => $ExpInfo[0]['ph_no'], "isdeleted" => 0)));
        $r = $this->model->updateItem($field, $where_fields, 'orders');

        $where = array("AND" => array("=" => array('ph_no' => $ExpInfo[0]['ph_no'])));
        $UserInfo = $this->model->getItemByOther( $where, 'orders');

        # 数据库更新成功
        if($r && $e){
            # 新增配送信息
            $whereExpressData = array("AND" => array("=" => array('shop_express_no'=>$ExpInfo[0]['shop_express_no'], "isdeleted"=>0)));
            $expDataInfo = $this->model->getItemByOther($whereExpressData,'express_data');
            if($expDataInfo){
                # 新增配送信息
                $status = 4;
                $express_order['shop_express_no'] = $ExpInfo[0]['shop_express_no'];
                $express_order['express_order_no'] = $ExpInfo[0]['order_no'];
                $express_order['express_type'] = $ExpInfo[0]['express_type'];
                $expd = $this->model->addExpressData($express_order,$status);
                if($expd){
                    $fieldExpData = array();
                    $fieldExpData['express_code'] = 200;
                    $fieldExpData['express_code_desc'] = '自建骑手取单成功';
                    $fieldExpData['express_data'] = '';
                    $fieldExpData['result_code'] = 0;
                    $this->model->updateExpressData($expd,$fieldExpData);
                }
            }
            echo json_encode(['status'=>1,'msg'=>'确认取货成功','code'=>200]);
        }else{
            echo json_encode(['status'=>0,'msg'=>'系统订单状态更新失败','code'=>3004]);
        }
    }




    /**
     * 订单送达
     */
    public function finishAction(){
        $express_id = (!empty($this->params['express_id'])) ? (int) $this->params['express_id'] : '';
        $finishselect = (!empty($this->params['finishselect'])) ? (int) $this->params['finishselect'] : '';       # 订单完成签收人状态
        $remark = (!empty($this->params['remark'])) ? $this->params['remark'] : '';   # 订单备注

        if(!$express_id || !$finishselect ){
            echo json_encode(['status'=>0,'msg'=>'参数错误','code'=>3003]);exit;
        }
        $whereExp = ['AND'=>['='=>['id'=>$express_id]]];
        $ExpInfo = $this->model->getItemByOther($whereExp,'orders_express');
        if(!$ExpInfo){
            echo json_encode(['status'=>0,'msg'=>'读取数据失败','code'=>3003]);exit;
        }
        if($finishselect == 6){
            $status = $finishselect;
            $express_code = 6001;
            $express_code_desc = '订单配送异常';
        }else{
            $status = 5;
            $express_code = 200;
            $express_code_desc = '自建骑手配送完成';
        }
        $field['updatedt'] = 'now()';
        $field['status'] = $status;
        $field['finish_select'] = $finishselect;
        $field['error_remark'] = $remark;
        $where_fields = array("AND" => array("=" => array('id' => $express_id, "isdeleted" => 0)));
        $e = $this->model->updateItem($field, $where_fields, 'orders_express');


        $field = array();
        $field['updatedt'] = 'now()';
        if($finishselect == 6){
            $field['status'] = $finishselect;
        }else{
            $field['status'] = 5;
        }
        $where_fields = array("AND" => array("=" => array('ph_no' => $ExpInfo[0]['ph_no'], "isdeleted" => 0)));
        $r = $this->model->updateItem($field, $where_fields, 'orders');


        if($r && $e){
            # 新增配送信息
            $whereExpressData = array("AND" => array("=" => array('shop_express_no'=>$ExpInfo[0]['shop_express_no'], "isdeleted"=>0)));
            $expDataInfo = $this->model->getItemByOther($whereExpressData,'express_data');
            if($expDataInfo){
                # 新增配送信息
                $express_order['shop_express_no'] = $ExpInfo[0]['shop_express_no'];
                $express_order['express_order_no'] = $ExpInfo[0]['order_no'];
                $express_order['express_type'] = $ExpInfo[0]['express_type'];
                $expd = $this->model->addExpressData($express_order,$status);
                if($expd){
                    $fieldExpData = array();
                    $fieldExpData['express_code'] = $express_code;
                    $fieldExpData['express_code_desc'] = $express_code_desc;
                    $fieldExpData['express_data'] = '';
                    $fieldExpData['result_code'] = 0;
                    $this->model->updateExpressData($expd,$fieldExpData);
                }

            }
            echo json_encode(['status'=>1,'msg'=>'确认取货成功','code'=>200]);
        }else{
            echo json_encode(['status'=>0,'msg'=>'系统订单状态更新失败','code'=>3004]);
        }

    }


    /**
     * 常见问题
     */
    public function FQAAction()
    {
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = "fqa";
        $page = (!empty($this->params['page'])) ? (int) $this->params['page'] : 1;
        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
        $this->view->setVariable("number_page", $number_page);
        $this->view->setVariable("page", $page);
        $orderby = array('id' => 'asc');
        $where_fields['AND']['=']['status'] = 1;
        $where_fields['AND']['=']['isdeleted'] = 0;
        $expressFieldsColumn = '';
        $rsp_list = $this->model->getAll($where_fields, $table, $orderby, $expressFieldsColumn );
        $this->view->setVariable("rsp_list", $rsp_list);
        $this->view->setVariable("title", '常见问题');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    /**
     * 常见问题详情
     */
    public function FQADetailsAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = "fqa";
        $id = empty($this->params["fd"]) ? '0' : (int) $this->params["fd"];
        $where_fields = array("AND" => array("=" => array("id" => $id)));
        $row = $this->model->getItemById($id, $table);
        $this->view->setVariable("row", $row);
        $this->view->setVariable("title", '消息中心');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }




    /**
     * 消息中心
     */
    public function messageContenAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $sql = "SELECT * FROM news  WHERE (status = 9 OR status = 7 OR status = 3) AND riders_tel = '{$_SESSION['riders']['tel']}' AND isdeleted = 0  order by ico asc";
        $res = $this->model->getAny($sql);
        if (!empty($res)) {
            while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
                $rsp_list[] = $row;
            }
        }

        $count = count($rsp_list);
        $this->view->setVariable("count", $count);
        $this->view->setVariable("rsp_list", $rsp_list);
        $this->view->setVariable("title", '消息中心');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }


    /**
     * 消息详情
     */
    public function messageDetailsAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = "news";
        $id = empty($this->params["nd"]) ? '0' : (int) $this->params["nd"];
        $row = $this->model->getItemById($id, $table);

        $data['ico'] = 2;
        $data['updatedt'] = 'now()';
        $data['update_id'] = $_SESSION['riders']['uid'];
        $where_fields = array("AND" => array("=" => array('id' => $id)));
        $this->model->updateItem($data, $where_fields, $table);

        $this->view->setVariable("row", $row);
        $this->view->setVariable("title", '消息中心-详情');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }


    /**
     * 骑手认证
     */
    public function riders_approveAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $table = 'riders';
        $tel = $_SESSION['riders']['tel'];
        $where_fields = array("AND" => array("=" => array("tel" => $tel)));
        $ridersResult = $this->model->getItemByOther($where_fields, $table);

        $orderby = array('id' => 'asc');
        $like_fields = [];
        $where['AND']['=']['parent_code'] = 1;
        $where_city['AND']['=']['is_parent'] = 1;
        $where_city['AND']['>']['parent_code'] = 1;
        $where_area['AND']['=']['is_parent'] = 0;
        $where_area['AND']['>']['parent_code'] = 1;
        $province = $this->model->getAll($where,'area',$orderby,$like_fields);
        $city = $this->model->getAll($where_city,'area',$orderby,$like_fields);
        $area = $this->model->getAll($where_area,'area',$orderby,$like_fields);

        $storeSql = "SELECT store_id,storename from xd_store where (status = 2 or status = 4 or status = 5)  and isdeleted = 0";
        $store = $this->model->getAny($storeSql);
        if (!empty($store)) {
            while ($Stores = mysqli_fetch_array($store, MYSQLI_ASSOC)) {
                $row[] = $Stores;
            }
        }
        foreach ($ridersResult as &$v1){
            foreach ($province as $p){
                if($v1['province'] == $p['code']){
                    $v1['province_name'] = $p['fullname'];
                }
                if($v1['service_province'] == $p['code']){
                    $v1['service_province_name'] = $p['fullname'];
                }
            }
        }

        foreach ($ridersResult as &$v2){
            foreach ($city as $c){
                if($v2['city'] == $c['code']){
                    $v2['city_name'] = $c['fullname'];
                }
                if($v2['service_city'] == $c['code']){
                    $v2['service_city_name'] = $c['fullname'];
                }
            }
        }

        foreach ($ridersResult as &$v3){
            foreach ($area as $a){
                if($v3['area'] == $a['code']){
                    $v3['area_name'] = $a['fullname'];
                }
                if($v3['service_area'] == $a['code']){
                    $v3['service_area_name'] = $a['fullname'];
                }
            }
        }

        if($row) {
            foreach ($ridersResult as &$v4) {
                foreach ($row as $r) {
                    if ($v4['service_shop_no'] == $r['store_id']) {
                        $v4['shopname'] = $r['storename'];
                    }
                }
            }
        }

        $newCount = $this->model->getNewsCount();
        $this->view->setVariable("newCount", $newCount);
        $this->view->setVariable("ridersResult", $ridersResult[0]);
        $this->view->setVariable("tel", $_SESSION['riders']['tel']);

        $this->view->setVariable("title", '元祖骑手认证');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    /**
     * 骑手提交审核资料
     */
    public function riders_approveSubAction(){
        //        unset($_SESSION['riders']);
        $table = 'riders';
        $log=new Logging();
        $log->lfile('riderSub');
        $msg = ['status'=>0,'msg'=>''];
        if($this->params["tel"] != $_SESSION['riders']['tel']){
            $log->lwrite(date('Y-m-d H:i:s').'骑手电话-审核：'.$this->params["tel"].' 骑手电话-session：'.$_SESSION['riders']['tel']);
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }

        $where_fields = array("AND" => array("=" => array("tel" => $this->params["tel"])));
        $checkInfo = $this->model->checkRiders($this->params["name"],$this->params["username"]);
        if($checkInfo){
            $msg['msg'] = '用户名或昵称已存在';
            echo json_encode($msg);
            exit;
        }

        if (isset($_FILES['file0']) && $_FILES['file0']['name'] !== '') {
            $dir = $this->model->Uploaddir("riders/".$this->params['service_shop']);
             $upload_result = FileUpload::uploadSingle('file0', 'riders_'.time().'f', '', $dir);
             if ($upload_result != "") {
                 if ($upload_result == "filesize" || $upload_result == "fileext" || $upload_result == "fileupload") {
                     $msg['msg'] = '档案过大或格式不符';
                     echo json_encode($msg);
                     exit;
                 } else {
                     $field['card_front_img'] = $this->params['service_shop'].'/'.$upload_result;
                 }
             }
        }


        if (isset($_FILES['file02']) && $_FILES['file02']['name'] !== '') {
            $dir = $this->model->Uploaddir("riders/".$this->params['service_shop']);
            $upload_result = FileUpload::uploadSingle('file02', 'riders_'.time().'b', '', $dir);
            if ($upload_result != "") {
                if ($upload_result == "filesize" || $upload_result == "fileext" || $upload_result == "fileupload") {
                    $msg['msg'] = '档案过大或格式不符';
                    echo json_encode($msg);
                    exit;
                } else {
                    $field['card_back_img'] = $this->params['service_shop'].'/'.$upload_result;
                }
            }
        }

        if (isset($_FILES['file03']) && $_FILES['file03']['name'] !== '') {
            $dir = $this->model->Uploaddir("riders/".$this->params['service_shop']);
            $upload_result = FileUpload::uploadSingle('file03', 'riders_'.time().'h', '', $dir);
            if ($upload_result != "") {
                if ($upload_result == "filesize" || $upload_result == "fileext" || $upload_result == "fileupload") {
                    $msg['msg'] = '档案过大或格式不符';
                    echo json_encode($msg);
                    exit;
                } else {
                    $field['health_cards'] = $this->params['service_shop'].'/'.$upload_result;
                }
            }
        }
        $serviceCitys = $this->params["service_city"];
        $serviceCity = explode('_',$serviceCitys);
        $citys = $this->params["city"];
        $city = explode('_',$citys);
        $field['sex'] = $this->params["sex"];
        $field['birth_date'] = $this->params["birth_date"];
        $field['province'] = $city[0];
        $field['city'] = $city[1];
        $field['area'] = $city[2];
        $field['service_province'] = $serviceCity[0];
        $field['service_city'] = $serviceCity[1];
        $field['service_area'] = $serviceCity[2];
        $field['service_shop_no'] = $this->params["service_shop"];
        $field['jkz_end'] = $this->params["jkz_end"];
        $field['jkz_start'] = $this->params["jkz_start"];
        $field['name'] = $this->params["name"];
        $field['username'] = $this->params["username"];
        $field['age'] = $this->params["age"];
        $field['tel'] = $this->params["tel"];
        $field['address'] = $this->params["address"];
        $field['urgency_cont_username_tel'] = $this->params["urgency_cont_username_tel"];
        $field['urgency_relation'] = $this->params["urgency_relation"];
        $field['urgency_cont_username'] = $this->params["urgency_cont_username"];
        $field['resume'] = $this->params["resume"];
        $field['id_card'] = $this->params["id_card"];
        $field['status'] = 0;
        $field['initiate'] = 1;
        $log->lwrite(date('Y-m-d H:i:s').','.(json_encode($field,JSON_UNESCAPED_UNICODE)));
        $r = $this->model->updateItem($field, $where_fields, $table);
        if($r){
            $msg['status'] = 1;
            $msg['msg'] = '提交成功';
            echo json_encode($msg);
        }else{
            $msg['msg'] = '系统异常';
            echo json_encode($msg);
            exit;
        }
    }


    /**
     * 筛选区域下门店信息
     */
    public function getAreaShopAction(){
        $sql = "SELECT * FROM xd_store WHERE (status = 2 OR status = 4 OR status = 5) AND area = ".$_POST["area"];
//        $sql = "SELECT * FROM xd_store ";
        $store = $this->model->getAny($sql);
        if (!empty($store)) {
            while ($Stores = mysqli_fetch_array($store, MYSQLI_ASSOC)) {
                $row[] = $Stores;
            }
        }
        if($row){
            $area = [];
            foreach ($row as $k=>$r){
                $area[$k]['value'] = $r['store_id'];
                $area[$k]['text'] = $r['storename'];
            }
            echo json_encode(['status'=>1,'msg'=>'查找成功','code'=>200,'result'=>$area]);
        }else{
            echo json_encode(['status'=>0,'msg'=>'查找失败','code'=>4000]);
        }
    }

    /**
     * 成功信息提示
     */
    public function hintSucceedMessageAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }

        $this->view->setVariable("title", '资料审核中');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    /**
     * 失败错误信息提示
     */
    public function hintFailedMessageAction(){
        if (!isset($_SESSION['riders']['tel'])) {
            Tools::AlertTxt('?md=riders&cl=riders&at=regriders', '请先登录');
            exit;
        }
        $this->view->setVariable("title", '资料审核未通过');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function citydataAction()
    {
        $city = $this->model->cityData();
        $data1 = array();
        $data2 = array();
        $_temp2 = "";
        $_temp1 = "";
//        foreach ($city as $k => $v) {
//            $c3 = array('value' => $v['code3'], 'text' => Tools::transcoding($v['fullname3']));
//            $data[$v['code1']]['value'] = $v['code1'];
//            $data[$v['code1']]['text'] = Tools::transcoding($v['fullname1']);
//            $data[$v['code1']][$v['code2']]['value'] = $v['code2'];
//            $data[$v['code1']][$v['code2']]['text'] = Tools::transcoding($v['fullname2']);
//            $data[$v['code1']][$v['code2']]['children'][] = $c3;
//        }
        foreach ($city as $k => $v) {
            $c3 = array('value' => $v['code3'], 'text' => $v['fullname3']);
            $data[$v['code1']]['value'] = $v['code1'];
            $data[$v['code1']]['text'] = $v['fullname1'];
            $data[$v['code1']][$v['code2']]['value'] = $v['code2'];
            $data[$v['code1']][$v['code2']]['text'] = $v['fullname2'];
            $data[$v['code1']][$v['code2']]['children'][] = $c3;
        }
        foreach ($data as $k => $v) {
            foreach ($v as $key => $value) {
                if (is_array($value)) {
                    unset($data[$k][$key]);
                    $data[$k]['children'][] = $value;
                }
            }
            $data1[] = $data[$k];
        }
        echo json_encode($data1);
        exit;
    }

    public function getCanCelOrdersAction()
    {
        $show = $_SESSION['riders']['show'];
        if($show == 1){
            $_SESSION['riders']['show'] = 0;
            $type = (!empty($this->params['type'])) ? $this->params['type'] : '';

            $sql = "SELECT DISTINCT(order_no) AS orderNo FROM news  WHERE status = 7 AND ico = 1 AND riders_tel = '{$_SESSION['riders']['tel']}' AND isdeleted = 0  GROUP BY order_no order by updatedt desc";
            $news = $this->model->getAny($sql);

            if (!empty($news)) {
                while ($row = mysqli_fetch_array($news, MYSQLI_ASSOC)) {
                    $rsp_list[] = $row;
                }
            }

            if($rsp_list){
                foreach ($rsp_list as &$n){
                    $sqlo = "SELECT a.receiver_address,b.address  FROM orders as a  left join xd_store as b ON a.shop_no = b.store_id WHERE  a.order_no ='{$n['orderNo']}'";
                    $datas = $this->model->getAny($sqlo);
                    if (!empty($datas)) {
                        while ($row = mysqli_fetch_array($datas, MYSQLI_ASSOC)) {
                            $n['receiver_address'] = $row['receiver_address'];
                            $n['address'] = $row['address'];
                        }
                    }
                }
            }

            $count = count($rsp_list);
            $data = ['status'=>1,'msg'=>'您有 '.$count.' 订单被门店取消，请前往消息中心查看详情','result'=>$rsp_list,'num'=>$count];
            echo json_encode($data);
            exit;
        }

    }


    public function protocolAction()
    {
        $this->view->setVariable("title", '骑手协议');
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }
}

?>
